﻿// exercise_6.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>

//7-1 约分最简分式
int gcd(int m, int n)
{
	if (n)
		while ((m %= n) && (n %= m));

	return m + n;
}
void ex6_7_1()
{
	int denominator = 1;
	int numerator = 1;
	scanf("%d/%d", &numerator, &denominator);
	int factor = gcd(denominator, numerator);
	printf("%d/%d", numerator / factor, denominator / factor);
}

//7-2 念数字
void ex6_7_2()
{
	char ch;
	int first = 1;
	while ((ch = getchar()) != '\n') {
		if (!first) {
			printf(" ");
		}
		switch (ch) {
		case '-':printf("fu"); break;
		case '0':printf("ling"); break;
		case '1':printf("yi"); break;
		case '2':printf("er"); break;
		case '3':printf("san"); break;
		case '4':printf("si"); break;
		case '5':printf("wu"); break;
		case '6':printf("liu"); break;
		case '7':printf("qi"); break;
		case '8':printf("ba"); break;
		case '9':printf("jiu"); break;
		}
		first = 0;
	}
}

//7-3 验证“哥德巴赫猜想”
int is_prime(int n)
{
	if (n == 2) {
		return 1;
	}

	if (n <= 1 || n % 2 == 0) {
		return 0;
	}

	int limit = sqrt(n) + 1;
	for (int i = 3; i <= limit; i += 2) {
		if (n % i == 0) {
			return 0;
		}
	}

	return 1;
}
void ex6_7_3()
{
	int n, i, ok;
	scanf("%d", &n);

	for (i = 2; i <= n / 2; i++)
	{
		if (is_prime(i) && is_prime(n - i)) {
			printf("%d = %d + %d", n, i, n - i);
			break;
		}
	}
}
// 7-4 单词长度
void ex6_7_4()
{
	char ch;
	int first = 1;
	int word_length = 0;
	while (ch = getchar())
	{
		word_length = 0;
		while (ch != '.' && ch != ' ')
		{
			ch = getchar();
			word_length++;
		}

		if (word_length != 0)
		{
			if (first == 1)
				printf("%d", word_length);
			else
				printf(" %d", word_length);
			first = 0;
		}

		if (ch == '.')
			return;
	}
}

//7-5 英文字母替换加密
void ex6_7_5()
{
	char ch;
	while ((ch = getchar()) != '\n') {
		if (ch >= 'a' && ch <= 'z') {
			ch -= 31;
			ch = ch > 'Z' ? ch - 26 : ch;
		}
		else if (ch >= 'A' && ch <= 'Z') {
			ch += 33;
			ch = ch > 'z' ? ch - 26 : ch;
		}
		else {
			;
		}
		putchar(ch);
	}
	putchar('\n');
}

//7-6 二分法求多项式单根
double a0, a1, a2, a3;

double f(double x)
{
	return x * (x * (a3 * x + a2) + a1) + a0;
}

void ex6_7_6()
{
	double a, b, c;

	scanf("%lf%lf%lf%lf", &a3, &a2, &a1, &a0);
	scanf("%lf%lf", &a, &b);

	while (b - a > 0.01) {
		c = (a + b) / 2;

		if (f(a) * f(c) > 0)
			a = c;
		else
			b = c;
	}

	printf("%.2f", (a + b) / 2);
}

int main()
{
	int a = 5, b = 6;
	printf("%d", ++a == b-- ? ++a : --b);
}